package cn.lg.soar.system.account.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.core.annotations.ResourceScanIgnore;
import cn.lg.soar.system.account.model.SsoLoginDTO;
import cn.lg.soar.system.account.model.SsoNotifyClientLogout;
import cn.lg.soar.system.account.service.ISsoClientService;
import cn.lg.soar.system.api.constant.ModuleConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ConditionalOnProperty(name = "lg.sso.client.enabled", havingValue = "true")
@ResourceScanIgnore
@Api(tags = {"单点登录客户端"})
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/sso/client")
public class SsoClientController {

    @Autowired
    private ISsoClientService ssoService;

    /**
     * 给前端访问（单点登录）
     * @param dto
     * @return
     */
    @ApiOperation("登录验证")
    @PostMapping("validation")
    public HttpResult<String> validation(@RequestBody SsoLoginDTO dto) {
        return HttpResult.data(
                this.ssoService.validation(dto)
        );
    }

    /**
     * 接收服务端通知
     * @param ssoLogout
     * @return
     */
    @ApiOperation("退出登录通知")
    @GetMapping("logout-notify")
    public HttpResult<Void> logout(SsoNotifyClientLogout ssoLogout) {
        AssertUtil.notEmpty(ssoLogout.getUsername(), "缺少username");
        ssoService.logout(ssoLogout);
        return HttpResult.success();
    }

}
